<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Api\Controllers\Auths;

use App\Api\BaseApi;
use App\Models\GroupOrder;
use App\Models\PointReleaseRecord;
use App\Models\ProductOrder;
use App\Service\Setting\BeanSettingService;

class DataAnalysisApi extends BaseApi
{
    public function myWallet()
    {
        $user = self::UserModel([
            'pointAccount',
            'beanAccount',
            'walletAccount',
            'rewardAccount',
            'performanceLevel',
            'userLevel',
        ]);
        $order = ProductOrder::query()->where('user_id', $user->id)
            ->where('status', '>', 2)
            ->get();
        $group = GroupOrder::query()->where('user_id', $user->id)
            ->where('status', '>', 2)
            ->get();
        $pointRecord = PointReleaseRecord::query()->where('user_id', $user->id)
            ->sum('amount_released');
        $res = [
            'wallet_account' => $user->walletAccount->balance,
            'reward_account' => $user->rewardAccount->available_balance,
            'bean_account' => $user->beanAccount->available_beans,
            'bean_prince' => BeanSettingService::make()->getSetting('PRICE'),
            'point_account' => $user->pointAccount->available_points,
            'shopping_points' => $user->contribution_value,
            'level' => $user->userLevel->level ?? 0,
            'performanceLevel' => $user->performanceLevel->name ?? 'Lv0',
            'consumption' => bcadd($order->sum('total_amount'), $group->sum('amount'), 2),
            'total_revenue_of_points' => $pointRecord,
            'group_success_order' => $group->count(),
            'product_success_order' => $order->count(),
        ];
        return responses($res);
    }
}
